import yaml
import os
from utils.handle_path import *

# 封装操作yaml文件的工具类
class YamlData():
    def __init__(self, file):
        if os.path.isfile(file):
            self.file = file
        else:
            raise FileNotFoundError("文件不存在")

    #  读取yaml数据
    @property
    def r_data(self) -> dict :
        with open(file=self.file, mode="r",encoding='gbk') as file:
            data = yaml.load(file, Loader=yaml.FullLoader)
            # print(type(data))
        return data

    #  追加写入yaml数据
    def a_data(self,message):
        with open(file=self.file, mode="a",encoding='utf-8') as file:
            data = yaml.dump(message,file)
        return data
        # print(data)

    # 修改yaml文件数据，为关联接口使用，注意key值要以关键字参数的形式传入可变长元组中,按照层级顺序传值
    def w_data(self,*args,new_value=None):
        with open(file=self.file, mode="r",encoding='utf-8') as file:
            old_data = yaml.load(file, Loader=yaml.FullLoader)
            print(old_data)
            # print(len(args))
            if len(args) == 1:
                # for key,value in old_data.items():
                    # print(type(key))
                    # print(key,value)
                print(old_data[args[0]])
                print(new_value)
                old_data[args[0]]=new_value
                with open(file=self.file, mode="w", encoding="utf-8") as replace_file:
                    # sort_keys=False  使yaml文件格式保持原来的排序
                    #allow_unicode=True   生成后的yaml中文乱码解决
                    yaml.dump(old_data,replace_file,sort_keys=False,allow_unicode=True)
                    with open(file=self.file, mode="r", encoding='utf-8') as new_file:
                        new_data = yaml.load(new_file, Loader=yaml.FullLoader)
                        print(new_data)
                    # else:
            # else:
            #     print(args)
            #     # print(new_value)
            #     a = len(args)
            #     # print(a)
            #     b = args[0]
            #     print(b)
            #     print(type(b))
            #     # for j in a:
            #     for i in args[1:]:
            #
            #
            #         b = old_data[b][i]
            #     print(b)
            #

            else:
                print(args)
                # print(new_value)
                a = len(args)
                # print(a)
                # b = args[0]
                # print(b)
                # print(type(b))
                # for j in a:
                q = 'old_data'
                for i in args[:a]:
                        q = q+"['{}']".format(i)
                        # args = old_data[i]
                        #
                        # q = q+a
                print(q)
                c = q+'=new_value'
                # ppp = eval(q)
                print(c)
                exec(c)
                print(old_data)
                with open(file=self.file, mode="w", encoding="utf-8") as replace_file:
                    # sort_keys=False  使yaml文件格式保持原来的排序
                    #allow_unicode=True   生成后的yaml中文乱码解决
                    yaml.dump(old_data,replace_file,sort_keys=False,allow_unicode=True)
                    with open(file=self.file, mode="r", encoding='utf_8') as new_file:
                        new_data = yaml.load(new_file, Loader=yaml.FullLoader)
                        print(new_data)

# old_data[]

            # else:
            #     pass

            # return data

    #清除yaml文件
    @property
    def clear_data(self) -> None :
        with open(file=self.file, mode="w",encoding='utf-8') as file:
            file.truncate()
        return




if __name__ == '__main__':
    yamlPath = os.path.join(base_path+'/001.yml')
    b = {'a':1}
    # data = YamlData(yamlPath).w_data('handlers','fileHandler','level',new_value='niubi')
    data = YamlData(yamlPath).r_data
    print(data)
